import Axios from 'axios'

const state = {
	loading: [true],	//加载动画
	tableData: [],		//单据表格数据
	status: [],			//当前按钮按钮状态
	rowId: null,		//当前操作的单据id
    taskId: null,       //任务id
	multipleSelection: [],
	selectRowId: []
}

const mutations = {
	updateLoading(state, bool) {		//更新加载动画
		state.loading.splice(0, 1, bool);
	},
	updateTableData(state, arr) {		//更新表格数据
		let tableData = state.tableData;
		tableData.length = 0;
		arr.length > 0 ? tableData.splice(0, 1, ...arr) : tableData.splice(0);
	},
	updateStatus(state, n) {            //更改状态
        state.status.splice(0, 1, n)
    },
    updateRowId(state, id){				//更改当前要操作的单据id
        state.rowId = id ? id : null;
    },
    updateTaskId(state, id) {
        state.taskId = id;
    },
    multipleSelection(state, arr) {
        state.multipleSelection.length = 0;
        state.selectRowId.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => {
                state.multipleSelection.push(item);
                state.selectRowId.push(item['id']);
            });
        } else {
            state.multipleSelection.splice(0);
            state.selectRowId.splice(0);
        }
    }
}

const actions = {
	//通过切换不同按钮状态获取对应的表格数据
	getOrderTableDataByBtnStutas({state, commit}, url = 'activiti/getNotApprovalTasks?billId=1&scheduleState=' + state.status[0]) {
		commit('updateLoading', true);
        Axios.get(url).then(res => {
            console.log(res);
            if(res.data.code == 20001){
            	setTimeout(() => {
            		if(res.data.data){
	                    commit('updateTableData', res.data.data);
	                }
	                commit('updateLoading', false);
            	}, 250);
            } else {
                setTimeout(() => {
            		commit('updateTableData', []);
	                commit('updateLoading', false);
            	}, 250);
            }
        }).catch(() => {
        	setTimeout(() => {
        		commit('updateTableData', []);
                commit('updateLoading', false);
        	}, 250);
        });
	}
}


export default {
	namespaced: true,
	state,
	mutations,
	actions
}